package com.zcxy.config;

import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zcxy.constant.CommonConstant;
import com.zcxy.mybatis.bean.SysUser;
import com.zcxy.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.Properties;

/**
 * mybatis拦截器，自动注入创建人、创建时间、修改人、修改时间
 *
 * @Author scott
 * @Date 2019-01-19
 */
@Slf4j
@Component
@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
public class MybatisInterceptor implements Interceptor {


    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        String sqlId = mappedStatement.getId();
        log.debug("------sqlId------" + sqlId);
        SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
        Object parameter = invocation.getArgs()[1];
        log.debug("------sqlCommandType------" + sqlCommandType);

        if (parameter == null) {
            return invocation.proceed();
        }
        if (SqlCommandType.INSERT == sqlCommandType) {
            Field[] fields = ReflectUtil.getFields(parameter.getClass());
            for (Field field : fields) {

                if (!("createBy".equals(field.getName()) || "createTime".equals(field.getName()) || "updateBy".equals(field.getName()) || "updateTime".equals(field.getName()) || "delFlag".equals(field.getName()))) {
                    continue;
                }
                log.debug("------field.name------" + field.getName());
                try {
                    // update-begin--Author:scott Date:20190828 for：关于使用Quzrtz 开启线程任务， #465
                    // 获取登录用户信息
                    SysUser sysUser = JwtUtil.getLoginUser();
                    System.out.println(field.getName());
                    // 获取当前的用户
                    if ("createBy".equals(field.getName())) {
                        field.setAccessible(true);
                        Object local_createBy = field.get(parameter);
                        field.setAccessible(false);
                        if (local_createBy == null || local_createBy.equals("")) {
                            String createBy = "admin";
                            if (sysUser != null) {
                                // 登录账号
                                createBy = sysUser.getLoginName();
                            }
                            if (StringUtils.isNotEmpty(createBy)) {
                                field.setAccessible(true);
                                field.set(parameter, createBy);
                                field.setAccessible(false);
                            }
                        }
                    }
                    // 注入创建时间
                    else if ("createTime".equals(field.getName())) {
                        field.setAccessible(true);
                        Object local_createDate = field.get(parameter);
                        field.setAccessible(false);
                        if (local_createDate == null || local_createDate.equals("")) {
                            field.setAccessible(true);
                            field.set(parameter, new Date());
                            field.setAccessible(false);
                        }
                    } else if ("updateBy".equals(field.getName())) {
                        field.setAccessible(true);
                        Object local_updateBy = field.get(parameter);
                        field.setAccessible(false);
                        if (local_updateBy == null || local_updateBy.equals("")) {
                            String updateBy = "admin";
                            if (sysUser != null) {
                                // 登录账号
                                updateBy = sysUser.getLoginName();

                            }
                            if (StringUtils.isNotEmpty(updateBy)) {
                                field.setAccessible(true);
                                field.set(parameter, updateBy);
                                field.setAccessible(false);
                            }
                        }
                    } else if ("updateTime".equals(field.getName())) {
                        field.setAccessible(true);
                        Object local_updateDate = field.get(parameter);
                        field.setAccessible(false);
                        if (local_updateDate == null || local_updateDate.equals("")) {
                            field.setAccessible(true);
                            field.set(parameter, new Date());
                            field.setAccessible(false);
                        }
                    } else if ("delFlag".equals(field.getName())) {
                        field.setAccessible(true);
                        Object local_delFlag = field.get(parameter);
                        field.setAccessible(false);
                        if (local_delFlag == null || local_delFlag.equals("")) {
                            field.setAccessible(true);
                            field.set(parameter, CommonConstant.DEL_FLAG_1);
                            field.setAccessible(false);
                        }
                    }
                } catch (Exception e) {
                }
            }
        }
        if (SqlCommandType.UPDATE == sqlCommandType) {
            Field[] fields = ReflectUtil.getFields(parameter.getClass());

            for (Field field : fields) {
                if (!("updateBy".equals(field.getName()) || "updateTime".equals(field.getName()))) {
                    continue;
                }
                log.debug("------field.name------" + field.getName());

                SysUser sysUser = JwtUtil.getLoginUser();


                if ("updateBy".equals(field.getName())) {
                    field.setAccessible(true);
                    Object local_updateBy = field.get(parameter);
                    field.setAccessible(false);
                    if (local_updateBy == null || local_updateBy.equals("")) {
                        String updateBy = "admin";
                        if (sysUser != null) {
                            // 登录账号
                            updateBy = sysUser.getLoginName();
                        }
                        if (StringUtils.isNotEmpty(updateBy)) {
                            field.setAccessible(true);
                            field.set(parameter, updateBy);
                            field.setAccessible(false);
                        }
                    }
                } else if ("updateTime".equals(field.getName())) {
                    field.setAccessible(true);
                    field.set(parameter, new Date());
                    field.setAccessible(false);
                }
            }
        }

        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        // TODO Auto-generated method stub
    }

}
